<?php

namespace App\Admin\Forms\Config;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;
use App\Models\Config as Model;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class CourierFee extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         $input['cash_dates'] = request()->input("cash_dates");
         $res = 0;
         if ($input){
             try {
                 foreach ($input as $key=>$value){
                     $model = Model::where('name',$key)->first();
                     $model->value = is_array($value)?json_encode($value):$value;
                     $res += $model->save();
                 }
             }catch (ModelNotFoundException|Exception $e){
                 return $this->response()->error($e->getMessage())->refresh();
             }
         }
         return $this->response()->success($res?'保存成功.':'暂无修改.')->refresh();
    }


    /**
     * Build a form here.
     */
    public function form()
    {
//        $this->
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return $this->getDefault();
    }

    protected function getDefault(){
        return Model::query()->pluck("value","name")->toArray();
    }
}
